package com.nwpu.controller;

import com.nwpu.common.Result;
import com.nwpu.domain.Goods;
import com.nwpu.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author DPH
 * @date 2023/5/29
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("{goodsId}")
    public Result<Goods> getGoodsById(@PathVariable String goodsId) {
        Goods goods = goodsService.getGoodsById(goodsId);
        return Result.of(goods);
    }

    @PutMapping
    public Result<Boolean> updateGoods(@RequestBody Goods goods) {
        Boolean res = goodsService.updateGoods(goods);
        return Result.of(res);
    }

    @PostMapping
    public Result<Boolean> addGoods(@RequestBody Goods goods) {
        Boolean res = goodsService.saveGoods(goods);
        return Result.of(res);
    }

    @DeleteMapping("{goodsId}")
    public Result<Boolean> deleteGoods(@PathVariable String goodsId) {
        Boolean res = goodsService.deleteGoodsById(goodsId);
        return Result.of(res);
    }
}
